import { Catch, HttpException, ArgumentsHost, ExceptionFilter, Logger } from "@nestjs/common";

@Catch(HttpException)
export class HttpExecptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request = ctx.getRequest();
    Logger.error(`${request.method} ${request.url}`, exception.stack, 'ExceptionFilter');
    response.status(exception.getStatus())
    .json({
      statusCode: exception.getStatus(),
      timestamp: new Date().toISOString(),
      path: request.url,
      methods: request.method,
      message: exception.message
    });
    }
}